package com.example.educationcommunity.service;

import com.example.educationcommunity.dto.CommentDTO;
import com.example.educationcommunity.dto.PagingDTO;
import com.example.educationcommunity.dto.QuestionDTO;
import com.example.educationcommunity.mapper.CommentMapper;
import com.example.educationcommunity.mapper.QuestionMapper;
import com.example.educationcommunity.mapper.UserMapper;
import com.example.educationcommunity.model.*;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private QuestionMapper questionMapper;

    public List<CommentDTO> getByParentIdList(Integer id) {
        //CommentDTO commentDTOs = new CommentDTO();
        CommentExample commentExample =new CommentExample();
        commentExample.createCriteria()
                .andParentIdEqualTo(id);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<CommentDTO> commentDTOList = new ArrayList<>();
        for(Comment comment:comments){
            User user = userMapper.selectByPrimaryKey(comment.getCommentator());
            CommentDTO commentDTO = new CommentDTO();
            BeanUtils.copyProperties(comment,commentDTO);
            commentDTO.setUser(user);
            commentDTOList.add(commentDTO);
        }
        //System.out.println(commentDTOList.get(0).getLikeCount());
        return commentDTOList;
    }

    public void createOrUpdate(Comment newComment) {
        newComment.setGmtCreate(System.currentTimeMillis());
        newComment.setGmtModified(System.currentTimeMillis());
        commentMapper.insertSelective(newComment);
    }

    public PagingDTO list(Integer userId, Integer page, Integer size) {
        PagingDTO paginationDTO = new PagingDTO();
        Integer totalPage;
        CommentExample example = new CommentExample();
        example.createCriteria()
                .andCommentatorEqualTo(userId);
        Integer totalCount=(int)commentMapper.countByExample(example);
        if(totalCount%size==0)
        {
            totalPage=totalCount/size;
        }
        else
        {
            totalPage=totalCount/size+1;
        }

        if(page<1) {
            page=1;
        }
        if(page>totalPage) {
            page=totalPage;
        }

        paginationDTO.setPagination(totalPage,page);

        Integer offset = size * (page - 1);
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria()
                .andCommentatorEqualTo(userId);
        List<Comment> comments = commentMapper.selectByExampleWithRowbounds(commentExample, new RowBounds(offset, size));

        List<CommentDTO> commentDTOList=new ArrayList<>();

        for(Comment comment:comments)
        {
            User user = userMapper.selectByPrimaryKey(comment.getCommentator());
            Question question = questionMapper.selectByPrimaryKey(comment.getParentId());
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.setId(comment.getId());
            BeanUtils.copyProperties(comment,commentDTO);
            commentDTO.setUser(user);
            commentDTO.setQuestion(question);
            commentDTOList.add(commentDTO);
        }
        paginationDTO.setComments(commentDTOList);
        return paginationDTO;
    }


    /**
     * 用count对方法计算回复的数量，缺陷是没有实现对数据库的写入
     * @param parentId
     * @return commentCount
     */
    public Integer countByParentId(int parentId) {
        //因为countByExample 返回的是LONG类型，但是在定义数据库的时候将count定义未Integer类型，所以临时在这里进行一下强制类型转换
        Long commentCountLong;
        Integer commentCountInt;
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria()
                .andParentIdEqualTo(parentId);
        commentCountLong = commentMapper.countByExample(commentExample);
        commentCountInt = commentCountLong.intValue();
        Integer commentCount = commentCountInt;

        return  commentCount;
    }
}
